//
//  LYCBCentralManagerDelegateProxy.swift
//  RxSwift_Practice
//
//  Created by 李永彬 on 2020/4/28.
//  Copyright © 2020 李永彬. All rights reserved.
//

import Foundation
import CoreBluetooth

//限定协议,限定为只能由类实现（class可以改为AnyObject）
internal protocol LYCBCentralManagerStateDelegate: class {
    func centralManagerDidUpdateState(_ central: CBCentralManager)
}

internal protocol LYCBCentralManagerDiscoveryDelegate: class {
    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)
}

internal protocol LYCBCentralManagerConnectionDelegate: class {
    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
    func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?)
    func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?)
}

internal protocol LYCBCentralManagerRestoreDelegate: class {
    func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any])
}

internal class LYCBCentralManagerDelegateProxy: NSObject, CBCentralManagerDelegate {
    
    // MARK: Initialization
    init(stateDelegate: LYCBCentralManagerStateDelegate, discoveryDelegate: LYCBCentralManagerDiscoveryDelegate, connectionDelegate: LYCBCentralManagerConnectionDelegate, restoreDelegate: LYCBCentralManagerRestoreDelegate) {
        self.stateDelegate = stateDelegate
        self.discoveryDelegate = discoveryDelegate
        self.connectionDelegate = connectionDelegate
        self.restoreDelegate = restoreDelegate
        super.init()
    }
    
    // MARK: Properties

    internal weak var stateDelegate: LYCBCentralManagerStateDelegate?
    internal weak var discoveryDelegate: LYCBCentralManagerDiscoveryDelegate?
    internal weak var connectionDelegate: LYCBCentralManagerConnectionDelegate?
    internal weak var restoreDelegate: LYCBCentralManagerRestoreDelegate?
    
    internal func centralManagerDidUpdateState(_ central: CBCentralManager) {
        stateDelegate?.centralManagerDidUpdateState(central)
    }
    
    internal func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        discoveryDelegate?.centralManager(central, didDiscover: peripheral, advertisementData: advertisementData, rssi: RSSI)
    }
         
    internal func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        connectionDelegate?.centralManager(central, didConnect: peripheral)
    }
          
    internal func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
        connectionDelegate?.centralManager(central, didFailToConnect: peripheral, error: error)
    }
          
    internal func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
        connectionDelegate?.centralManager(central, didDisconnectPeripheral: peripheral, error: error)
    }
         
    internal func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) {
        restoreDelegate?.centralManager(central, willRestoreState: dict)
    }
}
